<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>云校通-教师常规详情</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<script src="<%=basePath %>/js/picker.js"></script>
<script src="<%=basePath %>/js/select.js"></script>
<body ontouchstart class="js-jf">
	<div class="weui_tab tab-bottom">
		<div class="weui_tab_bd">
			<div class="weui-header bg-blue"> 
		  		<div class="weui-header-left"> <a onclick="back()" href="javascript:;" class="icon icon-109 f-white">返回</a></div>
		    	<h1 class="weui-header-title">详情</h1>
		    	<div class="weui-header-right"> <a id="action" href="javascript:;" class="f-white">修改</a ></div>
		    </div>
			<div class="weui_cells weui_cells_form mt0">
				<div class="weui_cell">
		          	<div class="weui_cell_hd">
		          		<label class="weui_label">部门</label>
		          	</div>
	         		<div class="weui_cell_bd weui_cell_primary no_icon">
						<input id="depart" type="text" class="weui_input tr" placeholder ="请输入部门" disabled="disabled" />
	      			</div>
	      			<input type="hidden" id="id"/>
	    		</div>
	    	</div>
			<div class="weui_cells weui_cells_form mt0">
				<div class="weui_cell">
          			<div class="weui_cell_hd">
            			<label class="weui_label label3">评分类别</label>
          			</div>
          			<div class="weui_cell_bd weui_cell_primary hide" id="typeConf">
						<input type="text" class="weui_input weui_input-select main1" value="请设置评分类别" readonly="readonly">
	          		</div>
	      		</div>
		      	<ul class="lb_show" id="routineTypes">
		      	</ul>
		      	<input type="hidden" id="routineType"/>
				<input type="hidden" id="routineTypeScore"/>
		     </div>
		     <div class="weui_cells weui_cells_form mt0">
 				<div class="weui_cell">
		            <div class="weui_cell_hd">
		              <label class="weui_label label3">值班组选择</label>
		            </div>
		            <div class="weui_cell_bd weui_cell_primary">
  						<input type="text" id="groupIds" class="weui_input weui_input-select main1" value="请选择值班组" disabled="disabled"/>
            		</div>
 	      		</div>
			</div>
		</div>
	</div>
	
	<!-- 这里的div的class后面加上 display可以显示点击效果 -->
	<div class="hide_bd"></div>
	<div class="hb_choose type2">
		<h1>选择评分对象</h1>
		<div class="weui_tab kcfd-list1" id="tab4">
			<div class="weui_tab_bd pb0">
				<div class="weui_panel weui_tab_bd_item qingjia mt0" id="mt01" style="display: block;">
				      <div class="pdfx_box">
				      </div>
				      <div class="bg_white">
				      	<span>+增加</span>
				      </div>
				</div>
			</div>
		</div>
	       <div class="bg_white last_li">
			<strong><b class="cancel">取消</b></strong>
			<strong><b class="bg-blue f-white" id="sure2">确认</b></strong>
	       </div>
	</div>
	<!-- end -->		
	<template id="checker">
	 	<div id="checkboll">
	   		<div>
	     		<span></span>
	   		</div>
	 	</div>
	</template>
</body>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script>
    $(function(){
   	 	
	   	$('#typeConf').click(function(){
	 		$('.hide_bd').attr("style", "display: block");
	 		$('.type2').show();
	 		loadMaskRoutineType();
	 	});
   	 	$("#action").click(function() {
   	 		var text = $(this).html();
   	 		if(text == '修改') {
   	 			$("#depart").attr("disabled",null);
   	 			$("#groupIds").attr("disabled",null);
   	 			$("#typeConf").show();
   	 			$(this).html("保存");
   	 		}else if(text == "保存") {
   	 			typeSubmit();
   	 		}
   	 	});
	   	$('.type2').find('span').click(function(){
	 		$('.type2').find('.pdfx_box').append(
	 			'<div class="pfdx2">\
					<p></p>\
					<input class="lb weui_input" placeholder="请输入评分类别">\
					<input class="lbfz weui_input" type="number" pattern="^[1-9][0-9]*" placeholder="如2分">\
					<i class="r_red"></i>\
		      </div>'
	 		);
	 		$('.pdfx_box').find('p').eq($('.pdfx_box').find('p').length-1).append('对象'+$('.pdfx_box').find('p').length);
	 		$('.pdfx_box').scrollTop( $('.pdfx_box')[0].scrollHeight);
	 	});
	   	$('.pdfx_box').mouseover(function(){
        	$('.pdfx_box').find('i').click(function(){
        		$(this).parent('.pfdx2').remove();
        		for(var i=0;i<$('.pdfx_box').find('.pfdx2').length-1;i++)
        		{
        			$('.pdfx_box').find('p').eq(parseInt($(this).parent('.pfdx2').find('p')[0].innerHTML.charAt(2))+i-1).empty();
        			var a = parseInt($(this).parent('.pfdx2').find('p')[0].innerHTML.charAt(2))+parseInt(i);
        			$('.pdfx_box').find('p').eq(parseInt($(this).parent('.pfdx2').find('p')[0].innerHTML.charAt(2))+i-1).append('对象'+ a);
        		}
        	});
        });
	   	$('.last_li .cancel').click(function(){
    		$('.hide_bd').attr("style", "display: none");
    		$('.type2').hide();
    		$('#action').html("修改");
    		$("#depart").attr("disabled", true);
    		$("#groupIds").attr("disabled",true);
 			$("#typeConf").hide();
    	});
	   	$(".type2 #sure2").click(function() {
	   		var routineType = "";
	   		var routineTypeScore = "";
	   		var isOk = true;
	   		var length = $(".pdfx_box").find(".pfdx2").length;
	   		for(var i = 0; i < length; i++) {
	   			var obj = $($(".pdfx_box").find(".pfdx2")[i]);
	   			var title = obj.find("p").html();
	   			var type = obj.find("input.lb").val();
	   			var score = obj.find("input.lbfz").val();
	   			if(type.trim().length == 0) {
	   				$.alert(title + "评分对象为空！", "");
	   				isOk = false;
	   				break;
	   			}
	   			if(!positiveValidate(score)) {
	   				$.alert(title + "分数格式不正确！", "");
	   				isOk = false;
	   				break;
	   			}
	   			routineType += type + ((i+1) < length ? "," : "");
	   			routineTypeScore += score + ((i+1) < length ? "," : "");
	   		}
	   		if(isOk) {
	   			loadRoutineType(routineType, routineTypeScore);
	   			$('.hide_bd').attr("style", "display: none");
		 		$('.type2').hide();
	   		}
	   	})
   	 	loadItem();
	});
    function listDel(thiz) {
    	$(thiz).remove();
    }
    function loadItem() {
    	var typeId = parseInt("${typeId}");
    	if(typeId < 1) {
    		$("#depart").attr("disabled", null);
    		$("#groupIds").attr("disabled",null);
 			$("#typeConf").show();
 			$('#action').html("保存");
 			loadGoupList("");
    		return;
    	}
    	$.ajax({
    		url: basePath + "/ls/student/points/type/detail/query",
    		type: 'POST',
    		dataType : "json",
    		data: {
    			typeId: "${typeId}"
    		},
    		success: function(data) {
    			if(data != null) {
    				$("#id").val(data.id);
    				$("#depart").val(data.depart);
    				loadRoutineType(data.routineType, data.routineTypeScore);
    				loadGoupList(data.groupIds);
    			}
    		},
    		error: function(data) {
    			alert("系统异常,请联系管理员");
    		}
    	});
    }
    function loadMaskRoutineType() {
    	var routineType = $("#routineType").val();
    	var routineTypeScore = $("#routineTypeScore").val();
    	var routineTypeHtml = "";
    	if(routineType != null && routineType.length > 0) {
    		var routineTypes = routineType.split(",");
			var routineTypeScores = new Array();
			if(routineTypeScore != null) {
				routineTypeScores = routineTypeScore.split(",");
			}
			for(var i in routineTypes) {
				var itm = routineTypes[i];
				var point = "";
				if(routineTypeScores.length > i) {
					point = parseInt(routineTypeScores[i]);
				}
				routineTypeHtml += '<div class="pfdx2">\
					<p>对象'+(parseInt(i)+1)+'</p>\
					<input class="lb weui_input" value="'+itm+'" placeholder="请输入评分类别">\
					<input class="lbfz weui_input" value="'+point+'" type="number" pattern="^[1-9][0-9]*" placeholder="如2分">\
					'+(i > 0 ? '<i class="r_red"></i>' : '')+'\
		      </div>';
			}
    	}else {
    		routineTypeHtml = '<div class="pfdx2">\
					<p>对象1</p>\
					<input class="lb weui_input" placeholder="请输入评分类别">\
					<input class="lbfz weui_input" type="number" pattern="^[1-9][0-9]*" placeholder="如2分">\
		      </div>';
    	}
    	$(".pdfx_box").html(routineTypeHtml);
    }
    function loadRoutineType(routineType, routineTypeScore) {
    	$("#routineType").val(routineType);
    	$("#routineTypeScore").val(routineTypeScore);
    	if(routineType != null) {
			var routineTypes = routineType.split(",");
			var routineTypeScores = new Array();
			if(routineTypeScore != null) {
				routineTypeScores = routineTypeScore.split(",");
			}
			var routineTypeHtml = "";
			for(var i in routineTypes) {
				var itm = routineTypes[i];
				var point = "";
				if(routineTypeScores.length > i) {
					point = routineTypeScores[i] + "分";
				}
				routineTypeHtml += "<li><p>" + itm + "</p><span>"+ point +"</span></li>";
			}
			$("#routineTypes").html(routineTypeHtml);
		}
    }
    function loadGoupList(groupIds) {
		$.ajax({
    		url: basePath + "/ls/student/points/group/list/query",
    		type: 'POST',
    		dataType : "json",
    		success: function(data) {
    			if(data != null) {
    				var map = new Map();
    				var items = [];
    				for(var i in data) {
    					map.set(data[i].id, data[i].groupName);
    					items.push({title: data[i].groupName, value: data[i].id});
    				}
    				if(groupIds.trim().length > 0) {
    					var array = groupIds.split(",");
    					var groupName = "";
    					for(var i in array) {
    						groupName += map.get(parseInt(array[i])) + ((parseInt(i)+1) < array.length ? "," : "");
    					}
    					$("#groupIds").val(groupName);
    					$("#groupIds").attr("data-values", groupIds);
    				}
    				$("#groupIds").select({
    					title: "请选择值班组",
    			        multi: true,
    			        split:',',
    			        closeText:'完成',
    			        items: items
    				});
    			}
    		},
    		error: function(data) {
    			alert("系统异常,请联系管理员");
    		}
    	});
	}
   function typeSubmit() {
	   var depart = emptyToNull($("#depart").val());
	   if(depart == null) {
		   $.alert("部门不能为空","");
		   return;
	   }
	   var routineType = emptyToNull($("#routineType").val());
	   if(routineType == null) {
		   $.alert("评分类别不能为空","");
		   return;
	   }
	   var groupIds = emptyToNull($("#groupIds").attr("data-values"));
	   if(groupIds == null) {
		   $.alert("值班组不能为空","");
		   return;
	   }
	   var routineTypeScore = emptyToNull($("#routineTypeScore").val()); 
	   $.ajax({
   		url: basePath + "/ls/student/points/type/addUpdate",
   		type: 'POST',
   		dataType : "json",
   		data: {
   			id: emptyToNull($("#id").val()),
   			depart: depart,
   			routineType: routineType,
   			routineTypeScore: routineTypeScore,
   			groupIds: groupIds
   		},
   		success: function(data) {
   			if (data.code == '000') {
   				$.toast("保存成功");
   				window.location.href = basePath + "/ls/student/points/typeList";
   			}else {
   				$.toast(data.msg, "cancel");
   			}
   		},
   		error: function(data) {
   			alert("系统异常,请联系管理员");
   		}
   	});
   }
</script>
</html>